//
//  HomePageViewController.m
//  ClassRoom
//
//  Created by apple on 12-11-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HomePageViewController.h"
#import "HomePageTableViewCell.h"
#import "CacheManager.h"
#import "PreferentialViewController.h"
#import "CoursesSearchViewController.h"
#import "UserCenterViewController.h"
#import "RegisterViewController.h"
#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface HomePageViewController ()

@end

@implementation HomePageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)registerNewUser
{
    RegisterViewController* registerViewController=[[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
    [self presentViewController:registerViewController animated:YES completion:nil];
}


-(void)logIn
{
    LoginViewController * logInViewController=[[[LoginViewController alloc]init] autorelease];
    [self presentViewController:logInViewController animated:YES completion:nil];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:23.0/250.f green:164.0/250.f blue:66.0/250.f alpha:1.0f];
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(registerNewUser)]autorelease];
    self.navigationItem.leftBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(logIn)]autorelease];
    
    UITableView *tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 170, 320, 246)] autorelease];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    _imageArray = [[NSArray arrayWithObjects:@"Home_Search_ImageView",@"Home_Person_ImageView",@"Home_Activity_ImageView", nil] retain];
    _titleArray = [[NSArray arrayWithObjects:@"搜课程",@"优惠活动",@"个人中心", nil] retain];
    
    isLogin = 0;
}

#pragma mark -
#pragma mark UITableView

- (void)setTableView{
    UITableView *tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44) style:UITableViewStylePlain] autorelease];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 246/3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    HomePageTableViewCell *cell = (HomePageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    
    if (cell == nil) {
        cell = [[[HomePageTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setCellData:[_imageArray objectAtIndex:indexPath.row] ButtonTitle:[_titleArray objectAtIndex:indexPath.row]];
    return cell;
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        CoursesSearchViewController *coursesSearchViewController=[[CoursesSearchViewController alloc]initWithNibName:@"CoursesSearchViewController" bundle:nil];
        [self.navigationController pushViewController:coursesSearchViewController animated:YES];
        
    }
    if (indexPath.row==1) {
        PreferentialViewController *preferentialViewController=[[PreferentialViewController alloc]initWithNibName:@"PreferentialViewController" bundle:nil];
        [self.navigationController pushViewController:preferentialViewController animated:YES];
        
    }
    if (indexPath.row==2) {
        UserCenterViewController *userCenterViewController=[[UserCenterViewController alloc]initWithNibName:@"UserCenterViewController" bundle:nil];
        [self.navigationController pushViewController:userCenterViewController animated:YES];
        
    }
}



- (void)viewDidUnload
{

    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [_imageArray release];
    [_titleArray release];
        
    [super dealloc];
}
@end
